package com.yun.zf.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.admin.mapper.ZfPerformanceMapper;
import com.yun.zf.admin.service.ZfPerformanceService;
import com.yun.zf.common.admin.dto.PerformanceDto;
import com.yun.zf.common.app.domain.ZfPerformance;
import com.yun.zf.common.admin.model.order.PerformanceMo;
import com.yun.zf.common.mapstruct.admin.PactMapper;
import com.yun.zf.system.service.ISysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 业绩分配表明细 服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-04-07
 */
@Service
@Slf4j
public class ZfPerformanceServiceImpl extends ServiceImpl<ZfPerformanceMapper, ZfPerformance> implements ZfPerformanceService {

    @Autowired
    private ISysDictDataService dictDataService;
    @Override
    public Boolean addPerformance(Long provCode,Long cityCode,Long orderId,BigDecimal amount,List<PerformanceMo> performanceMos) {
        // 数据转换
      List<ZfPerformance> performanceList =  PactMapper.INSTANCE.motoPerformance(performanceMos);
        for (ZfPerformance performance: performanceList) {
            performance.setProvinceCode(provCode);
            performance.setCityCode(cityCode);
            performance.setOrderId(orderId);
            String scale = dictDataService.getValue("allocation", performance.getRoleType());
            log.info("佣金比例---------{}",scale);
            performance.setAllocationAtio(new BigDecimal(scale)); // 佣金比例
            performance.setPerAmount(amount.multiply(new BigDecimal(scale)));
            //performance.setRoleName(); // 角色类型名称 前台送
        }

        return  this.saveBatch(performanceList);
    }

    @Override
    public List<PerformanceDto> getPerformances(Long provCode, Long cityCode, Long orderId) {

        return baseMapper.getPerformances(provCode,cityCode,orderId);
    }

    @Override
    public List<PerformanceMo> bringPerformances(Long provCode, Long cityCode, Long orderId) {
        return baseMapper.bringPerformances(provCode,cityCode,orderId);
    }

    @Override
    public int delPerFormance(Long provCode, Long cityCode, Long orderId) {
        QueryWrapper<ZfPerformance> wrapper = new QueryWrapper<>();
        wrapper.lambda().ne(ZfPerformance::getRoleType,"W") .eq(ZfPerformance::getOrderId,orderId).eq(ZfPerformance::getCityCode,cityCode).eq(ZfPerformance::getProvinceCode,provCode);
        return  baseMapper.delete(wrapper);
    }
}
